/**
 * @Title: history-file.js
 * @Description: history-file
 * @author: Fanchao
 * @date: 2021/2/10 9:54 下午
 * @version V1.0
 */
import React from 'react';
import styles from '@/pages/teaching-quality/class-evaluations/index.less';
import SectionTitle from 'components/SectionTitle';
import AddSomeButton from 'components/AddSomeButton';
import { message, Upload } from 'antd';
import {
    downloadHistoryDepartmentEvaluateScoreTemplate,
    downloadHistorySchoolExpertEvaluateScoreTemplate,
    downloadHistoryStudentEvaluateScoreTemplate,
} from 'api/index';
import * as FileSaver from "file-saver";
import axios from "axios";

const HistoryFile = ({ dispatch }) => {
    const downLoad1 = async () => {
        const res = await downloadHistoryStudentEvaluateScoreTemplate();
        let blob = new Blob([res.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' });
        FileSaver.saveAs(blob, res.filename);
    };
    const customRequest1 = async (option) => {
        const file = option.file;
        const formData = new FormData();
        formData.append('importFile', file);
        let config = {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        };
        axios.post("/file/importHistoryStudentEvaluateScore", formData, config).then((res) => {
            if (res.data.status === 'success') {
                message.success(`导入成功`);
                dispatch({
                    type: 'classEvaluations/getHistoryDataSource',
                    payload: {
                        center: '',
                        trainingType: '',
                        teachSection: '',
                        teacherNo: '',
                        teacherName: '',
                        courseName: '',
                        reqPageInfo: {
                            pageNumber: 1,
                            pageSize: 10,
                        },
                    },
                });
            } else {
                message.error(res.data.message);
            }
        }).then(res => {

        }).catch(err => {
        });
    };


    const downLoad2 = async () => {
        const res = await downloadHistorySchoolExpertEvaluateScoreTemplate();
        let blob = new Blob([res.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' });
        FileSaver.saveAs(blob, res.filename);
    };
    const customRequest2 = async (option) => {
        const file = option.file;
        const formData = new FormData();
        formData.append('importFile', file);
        let config = {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        };
        axios.post(`/file/importHistorySchoolExpertEvaluateScore`, formData, config).then((res) => {
            if (res.data.status === 'success') {
                message.success(`导入成功`);
                dispatch({
                    type: 'classEvaluations/getHistoryDataSource',
                    payload: {
                        center: '',
                        trainingType: '',
                        teachSection: '',
                        teacherNo: '',
                        teacherName: '',
                        courseName: '',
                        reqPageInfo: {
                            pageNumber: 1,
                            pageSize: 10,
                        },
                    },
                });
            } else {
                message.error(res.data.message);
            }
        }).then(res => {

        }).catch(err => {
        });
    };


    const downLoad3 = async () => {
        const res = await downloadHistoryDepartmentEvaluateScoreTemplate();
        let blob = new Blob([res.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' });
        FileSaver.saveAs(blob, res.filename);
    };
    const customRequest3 = async (option) => {
        const file = option.file;
        const formData = new FormData();
        formData.append('importFile', file);
        let config = {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        };
        axios.post(`/file/importHistoryDepartmentEvaluateScore`, formData, config).then((res) => {
            if (res.data.status === 'success') {
                message.success(`导入成功`);
                dispatch({
                    type: 'classEvaluations/getHistoryDataSource',
                    payload: {
                        center: '',
                        trainingType: '',
                        teachSection: '',
                        teacherNo: '',
                        teacherName: '',
                        courseName: '',
                        reqPageInfo: {
                            pageNumber: 1,
                            pageSize: 10,
                        },
                    },
                });
            } else {
                message.error(res.data.message);
            }
        }).then(res => {

        }).catch(err => {
        });
    };


    return (
        <div>
            <div className={styles.pageContainerHeader}>
                <SectionTitle title={'历史评估数据'}/>
                <div className={styles.btnGroup}>
                    <div className={styles.item}>
                        <div onClick={downLoad1} style={{ width: '220px', margin: '0 16px' }}>
                            <AddSomeButton title={'学员评教数据导入模版下载'}/>
                        </div>
                        <Upload
                            style={{ width: '220px' }}
                            fileList={[]}
                            customRequest={customRequest1}
                        >
                            <AddSomeButton title={'学员评教数据导入'}/>
                        </Upload>
                    </div>
                    <div className={styles.item}>
                        <div onClick={downLoad2} style={{ width: '220px', margin: '0 16px' }}>
                            <AddSomeButton title={'校专家评教数据导入模版下载'}/>
                        </div>
                        <Upload
                            style={{ width: '220px' }}
                            fileList={[]}
                            customRequest={customRequest2}
                        >
                            <AddSomeButton title={'校专家评教数据导入'}/>
                        </Upload>
                    </div>
                    <div className={styles.item}>
                        <div onClick={downLoad3} style={{ width: '220px', margin: '0 16px' }}>
                            <AddSomeButton title={'单位评教数据导入模版下载'}/>
                        </div>
                        <Upload
                            style={{ width: '220px' }}
                            fileList={[]}
                            customRequest={customRequest3}
                        >
                            <AddSomeButton title={'单位评教数据导入'}/>
                        </Upload>
                    </div>
                </div>
            </div>
        </div>
    );
};

export default HistoryFile;
